package com.aplink.generic.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

import com.aplink.generic.cache.MasterConfigManager;
import com.aplink.generic.database.DatabaseManager;
import com.aplink.generic.pojo.MasterConfig;
import com.aplink.generic.sharepreference.GenericSharePreference;

public class DataProvider extends ContentProvider {
	public static final String CLEAR_SYNC = "clearSync";
	public static final int CLEAR_SYNC_STATUS = 3;
	public static final String CLOUD_MESSAGE = "cloudMessage";
	public static final int GET_ALL = 1;
	public static final int GET_DISTINCT = 2;
	public static final int INSERT = 4;
	public static final String INSERT_TABLE = "Insert";
	public static final String TABLE = "table";
	private DatabaseManager mDatabaseManager;
	private final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

	@Override
	public int delete(final Uri uri, final String selection,
	        final String[] selectionArgs) {
		if (this.sUriMatcher.match(uri) == DataProvider.CLEAR_SYNC_STATUS) {
			final String table = uri.getLastPathSegment();
			final SQLiteDatabase db = this.mDatabaseManager
			        .getWritableDatabase();
			return db.delete(table, selection, null);
		}
		return 0;
	}

	@Override
	public String getType(final Uri uri) {
		return null;
	}

	@Override
	public Uri insert(final Uri uri, final ContentValues values) {
		if (this.sUriMatcher.match(uri) == INSERT) {
			final String table = uri.getLastPathSegment();
			SQLiteDatabase database = mDatabaseManager.getWritableDatabase();
			long id = database.insert(table, null, values);
			if (id != -1) {
				return Uri.parse(table + "/" + id);
			}
		}
		return null;
	}

	@Override
	public boolean onCreate() {
		if (GenericSharePreference.getInstance(getContext())
		        .getMasterConfigFileID() == 0) {
			return false;
		}
		MasterConfig mMasterConfig = MasterConfigManager.getInstance(
		        getContext()).get(
		        GenericSharePreference.getInstance(getContext())
		                .getMasterConfigFileID());
		if (mMasterConfig == null) {
			return false;
		}
		if (mMasterConfig.getAutoSync() != null) {
			final String authority = mMasterConfig.getAutoSync()
			        .getAuthorities();
			if (!TextUtils.isEmpty(authority)) {
				this.sUriMatcher.addURI(authority, DataProvider.TABLE + "/*",
				        DataProvider.GET_ALL);
				this.sUriMatcher.addURI(authority, DataProvider.TABLE + "/*/#",
				        DataProvider.GET_DISTINCT);
				this.sUriMatcher.addURI(authority, DataProvider.CLEAR_SYNC
				        + "/*", DataProvider.CLEAR_SYNC_STATUS);
				this.sUriMatcher.addURI(authority, INSERT_TABLE + "/*",
				        DataProvider.INSERT);
			}
		}
		this.mDatabaseManager = new DatabaseManager(getContext(), mMasterConfig);
		return true;
	}

	@Override
	public Cursor query(final Uri uri, final String[] projection,
	        final String selection, final String[] selectionArgs,
	        final String sortOrder) {
		if (this.sUriMatcher.match(uri) == DataProvider.GET_ALL) {
			final String table = uri.getLastPathSegment();
			final String queryString = SQLiteQueryBuilder.buildQueryString(
			        true, table, projection, selection, null, null, null, null);
			final SQLiteDatabase db = this.mDatabaseManager
			        .getReadableDatabase();
			return db.rawQuery(queryString, null);
		} else if (this.sUriMatcher.match(uri) == DataProvider.GET_DISTINCT) {
			final String table = uri.getPathSegments().get(1);
			final String queryString = SQLiteQueryBuilder.buildQueryString(
			        true, table, projection, selection, null, null, null, null);
			final SQLiteDatabase db = this.mDatabaseManager
			        .getReadableDatabase();
			return db.rawQuery(queryString, null);
		}
		return null;
	}

	@Override
	public int update(final Uri uri, final ContentValues values,
	        final String selection, final String[] selectionArgs) {
		if (this.sUriMatcher.match(uri) == DataProvider.CLEAR_SYNC_STATUS) {
			final String table = uri.getLastPathSegment();
			final SQLiteDatabase db = this.mDatabaseManager
			        .getWritableDatabase();
			return db.update(table, values, selection, selectionArgs);
		}
		return 0;
	}

}
